package com.foreveross.bsl.common.utils.balance.zookeeper2;

import java.util.Map;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.foreveross.bsl.common.utils.balance.ServiceListener;
import com.foreveross.bsl.common.utils.balance.ZkCfg;
import com.foreveross.bsl.common.utils.balance.ZkNode;

public class Test implements Runnable {
	
	private static final Logger logger = LoggerFactory.getLogger(Test.class);
	
	private String serverList = "localhost:2181";
	private int sessionTimeout;
	
	private String serverPath; // /mdm节点
	private static Map<String, String> serverMap; // zookeeper所有注册信息 /xx1 /xx2 ...
	
	private String registerAddress;
	private String registerData;
	private ZKServiceKeeper zksk;

	
	@Override
	public void run() {
		if (zksk == null) {
			zksk = new ZKServiceKeeper(serverList, sessionTimeout);
		}
		serverMap = zksk.getServiceMap(serverPath);
		
		if (logger.isInfoEnabled()) {
			logger.info("获取服务器注册列表：");
			Set<String> ips = serverMap.keySet();
			for (String ip : ips) {
				logger.info(" server={}，data={}", ip, serverMap.get(ip));
			}
		}
		
		// 为zonode 添加 监听器
		/*zksk.listen(serverPath, new ServiceListener() {

			private static final long serialVersionUID = 4987257103300290709L;

			@Override
			public void process(Map<String, String> newServicesMap) {
				serverMap = newServicesMap;
				if (logger.isInfoEnabled()) {
					logger.info("监听到服务器注册变化：");
					Set<String> ips = serverMap.keySet();
					for (String ip : ips) {
						logger.info(" server={}，data={}", ip, serverMap.get(ip));
					}
				}
			}

			@Override
			public void afterConnected() {
				logger.info("监听到服务器完成连接，重新注册信息并且更新监听数据。");
				if (!StringUtils.isBlank(registerAddress)) {
					zksk.register(serverPath, registerAddress, registerData);
				}
				serverMap = zksk.getServiceMap(serverPath);
			}
		});*/
	}
	
	public static void main(String[] args) {
		final ZKServiceKeeper zksk = new ZKServiceKeeper("localhost:2181", 3000);
		System.out.println(zksk.getData("/zilla/mam"));
		System.out.println(zksk.getData("/zilla/mam/localhost"));
		
		zksk.listen("/zilla", new ServiceListener() {

			private static final long serialVersionUID = 4987257103300290709L;

			@Override
			public void process(Map<String, String> newServicesMap) {
				serverMap = newServicesMap;
				if (logger.isInfoEnabled()) {
					System.out.println("监听到服务器注册变化：");
					Set<String> ips = serverMap.keySet();
					for (String ip : ips) {
						System.out.println("server=" + ip + "，data=" + serverMap.get(ip));
					}
				}
			}

			@Override
			public void afterConnected() {
				System.out.println("监听到服务器完成连接，重新注册信息并且更新监听数据。");
				serverMap = zksk.getServiceMap("/zilla");
			}
		});
		
		System.out.println(111);
		
		// ZKConfigKeeper zkck = new ZKConfigKeeper("localhost:2181", 3000);
		// System.out.println(zkck.getChildren("/zilla/mam"));
		
//		ZkNode znode = new ZkNode();
//		znode.setPath("/zilla");
//		adapterZnode(znode, zksk);
		
	}
	
	protected static void adapterZnode(ZkNode znode, ZKServiceKeeper zksk) {
		// 获取当前zookeeper所有节点信息
		Map<String, String> rootMap = zksk.getServiceMap(znode.getPath());
		if(rootMap.size() == 0) {
			znode.setLast(true);
			return;
		}
		ZkNode otherServiceNode;
		for (String path: rootMap.keySet()) {
			otherServiceNode = new ZkNode();
			otherServiceNode.setzParent(znode);
			otherServiceNode.setPath(znode.getPath() + ZkCfg.SEPARATOR + path);
			otherServiceNode.setName(path);
			otherServiceNode.setData(rootMap.get(path));
			znode.addzChilds(otherServiceNode);
			
			System.out.println("server=" + otherServiceNode.getPath() + "，data=" + otherServiceNode.getData());
			
			// 递归添加节点
			adapterZnode(otherServiceNode, zksk);
		}
	}  
}
